package com.example.warehouse_manager.controller;


import com.example.warehouse_manager.entity.*;
import com.example.warehouse_manager.page.Page;
import com.example.warehouse_manager.service.*;
import com.example.warehouse_manager.utils.TokenUtils;
import com.example.warehouse_manager.utils.WarehouseConstants;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.util.ResourceUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.FileNotFoundException;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;

@RestController
@RequestMapping("/product")
public class ProductController {

    @Autowired
    private StoreService storeService;

    @Autowired
    private BrandService brandService;

    @Autowired
    private ProductService productService;

    @Autowired
    private ProductTypeService productTypeService;

    @Autowired
    private SupplyService supplyService;

    @Autowired
    private PlaceService placeService;

    @Autowired
    private UnitService unitService;

    @Autowired
    private TokenUtils tokenUtils;

    //注入配置文件属性值
    @Value("${file.upload-path}")
    private String uploadPath;

    //查询所有仓库
    @RequestMapping("/store-list")
    public Result storeList(){
        List<Store> stores = storeService.queryAllStore();
        return Result.ok(stores);
    }

    //查询所有品牌
    @RequestMapping("/brand-list")
    public Result brandList(){
        List<Brand> brands = brandService.queryAllBrand();
        return Result.ok(brands);
    }

    //分页查询商品
    @RequestMapping("/product-page-list")
    public Result productListPage(Page page, Product product){
        page = productService.queryProductPage(page, product);
        return Result.ok(page);
    }

    //查询所有商品分类树
    @RequestMapping("category-tree")
    public Result loadTypeTree(){
        List<ProductType> productTypes = productTypeService.productTypeTree();
        return Result.ok(productTypes);
    }

    //查询所有供应商
    @RequestMapping("/supply-list")
    public Result supplyList(){
        List<Supply> supplies = supplyService.queryAllSupply();
        return Result.ok(supplies);
    }

    //查询所有产地
    @RequestMapping("/place-list")
    public Result placeList(){
        List<Place> places = placeService.queryAllPlace();
        return Result.ok(places);
    }

    //查询所有单位
    @RequestMapping("/unit-list")
    public Result unitList(){
        List<Unit> units = unitService.queryAllUnit();
        return Result.ok(units);
    }

    //上传图片
    @CrossOrigin//表示url接口允许被跨域请求
    @RequestMapping("/img-upload")
    public Result uploadImage(MultipartFile file){
        try {/*
            拿到图片上传到目录路径的File对象uploadPath:classpath:static/img/upload
            因为图片上传到的目录路径是类路径，所以不能直接将路径封装到File对象，使用类路径资源工具类ResourceUtils
            的方法getFile()来解析类路径，并且拿到封装了类路径的File对象
        */
            File uploadDirFile = ResourceUtils.getFile(uploadPath);
            //拿到图片上传到的目录路径的磁盘路径
            String uploadDirPath = uploadDirFile.getAbsolutePath();
            //拿到图片上传的名称
            String filename = file.getOriginalFilename();
            //拿到上传文件要保存到磁盘文件的路径
            String uploadFilePath = uploadDirPath+"//"+ filename;
            //上传图片
            file.transferTo(new File(uploadFilePath));
            //响应
            return Result.ok("图片上传成功");
        } catch (Exception e) {
            return Result.err(Result.CODE_ERR_BUSINESS,"图片上传失败");
        }
     }

     //添加商品
    @RequestMapping("/product-add")
    public Result addProduct(@RequestBody Product product,
                             @RequestHeader(WarehouseConstants.HEADER_TOKEN_NAME) String token){
        //拿到当前用户登录的id
        CurrentUser currentUser = tokenUtils.getCurrentUser(token);
        int createBy = currentUser.getUserId();
        product.setCreateBy(createBy);

        //执行业务
        Result result = productService.saveProduct(product);
        return result;
    }

    //修改商品上下架状态
    @RequestMapping("/state-change")
    public Result changeProductState(@RequestBody Product product){
        Result result = productService.updateStateByPid(product);
        return result;
    }

    //删除单个商品
    @RequestMapping("/product-delete/{productId}")
    public Result deleteProduct(@PathVariable Integer productId){
        return productService.deleteProductByIds(Collections.singletonList(productId));
    }

    //批量删除商品
    @RequestMapping("/product-list-delete")
    public Result deleteProductList(@RequestBody List<Integer> productIdList){
        return productService.deleteProductByIds(productIdList);
    }

    //修改商品
    @RequestMapping("/product-update")
    public Result updateProduct(@RequestBody Product product,
                                @RequestHeader(WarehouseConstants.HEADER_TOKEN_NAME) String token){
        //拿到当前用户登录的id
        CurrentUser currentUser = tokenUtils.getCurrentUser(token);
        int createBy = currentUser.getUserId();
        product.setCreateBy(createBy);

        return productService.setProductById(product);
    }

    //导出数据
    @RequestMapping("/exportTable")
    public Result exportTable(Page page, Product product){
        page = productService.queryProductPage(page, product);
        List<?> resultList = page.getResultList();
        //响应
        return Result.ok(resultList);
    }


}
